package com.donfelix.looknews.view.implpresenter;

import com.donfelix.looknews.api.ApiManager;
import com.donfelix.looknews.bean.zhihu.ZhihuStory;
import com.donfelix.looknews.view.implview.IZhihuStory;
import com.donfelix.looknews.view.implview.IZhihuStoryPresenter;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Don on 2017/3/29.
 * Desc
 */

public class ZhihuStoryPresenterImpl extends BasePresenterImpl implements IZhihuStoryPresenter {

    private IZhihuStory zhihuStory;

    public ZhihuStoryPresenterImpl(IZhihuStory zhihuStory) {
        if (zhihuStory == null) {
            throw new IllegalArgumentException("zhihuStory must not be null");
        }
        this.zhihuStory = zhihuStory;
    }

    @Override
    public void getZhihuStory(String id) {
        ApiManager.getInstance().getZhihuApiService()
                .getZhihuStory(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ZhihuStory>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(ZhihuStory story) {
                        zhihuStory.showStory(story);
                    }

                    @Override
                    public void onError(Throwable e) {
                        zhihuStory.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}
